package IMIC.NGUYENVANDOAN.XML_DEMO;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MODULE01_B02 extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MODULE01_B02 frame = new MODULE01_B02();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MODULE01_B02() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JButton btnNewButton = new JButton("MỞ FILES BẤT KỲ :D");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                
                JFileChooser objFile = new JFileChooser();
                objFile.setCurrentDirectory(new java.io.File("."));
                objFile.setDialogTitle("Chọn file: ");
                objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                //1. Định nghĩa danh sách các kiểu files mà chúng ta muốn làm việc
                String[] EXTENSION = new String[] { "xml" };
                
                //2. Thiết lập chế độ lọc files căn cứ vào danh sách nêu trên
                FileNameExtensionFilter filter = new 
                        FileNameExtensionFilter("XML files", EXTENSION);
                
                objFile.setFileFilter(filter);
                objFile.setMultiSelectionEnabled(false);

                //3. Lựa chọn kiểu cửa sổ làm việc:
                /*
                 * + showOpenDialog(): Mở files ...
                 * + showSaveDialog(): Lưu files ...
                 * */
                int returnVal = objFile.showOpenDialog(getParent());
                String ParentPath = "";
                String FileName = "";
                if (returnVal == JFileChooser.APPROVE_OPTION) {                    
                    ParentPath = objFile.getSelectedFile().getParent();       
                    JOptionPane.showMessageDialog(getParent(), 
                            "ParentPath: "+ ParentPath);
                    FileName = objFile.getSelectedFile().getName();
                    JOptionPane.showMessageDialog(getParent(), 
                            "FileName: "+ FileName);
               }
            }
        });
        btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 15));
        btnNewButton.setBounds(10, 11, 182, 40);
        contentPane.add(btnNewButton);
    }
}
